package com.zadtek.shopmall.activities;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsActivity;
import com.zadtek.shopmall.other.Const;
import com.zadtek.shopmall.utils.ToastUtil;

public class WebActivity extends AbsActivity {

    public static final String ABOUT_BLANK = "about:blank", URL = "URL", TITLE = "TITLE";

    private WebView web_simple_view;
    private ProgressBar pb_progressbar;

    private String mUrl;
    private String mTitle;
    private ImageView ivBack;
    private TextView title;


    @Override
    public int setView() {
        return R.layout.activity_web;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);

        final Intent intent = getIntent();
        mUrl = intent.getStringExtra(URL);
        mTitle = intent.getStringExtra(TITLE);

        ivBack = (ImageView) findViewById(R.id.ic_back);
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        title = (TextView) findViewById(R.id.title);
        web_simple_view = (WebView) findViewById(R.id.webview);
        pb_progressbar = (ProgressBar) findViewById(R.id.progressbar);
        if (!TextUtils.isEmpty(mTitle)) {
            title.setText(mTitle);
        }
        init();
    }

    private void init() {
        WebSettings settings = web_simple_view.getSettings();

        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);

        settings.setAllowFileAccess(true);

        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setBuiltInZoomControls(true);
        settings.setDomStorageEnabled(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            settings.setDisplayZoomControls(false);
        }

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
            settings.setRenderPriority(WebSettings.RenderPriority.HIGH);//提高渲染优先级
            settings.setSavePassword(false);
            settings.setPluginState(WebSettings.PluginState.ON);
        }

        pb_progressbar.setMax(100);
        //pb_progressbar.setProgressDrawable(new ColorDrawable(0x0090ff));
        pb_progressbar.setProgress(5); //先加载5%，以使用户觉得界面没有卡死

        web_simple_view.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                pb_progressbar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                pb_progressbar.setVisibility(View.GONE);
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                pb_progressbar.setVisibility(View.GONE);
            }
        });
        web_simple_view.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                if (TextUtils.isEmpty(mTitle)) {
                    setTitle(title);
                }
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress >= 5) {
                    pb_progressbar.setProgress(newProgress);
                }
            }
        });
//        int userID = AppPreferenceUtil.getUserId(this);
//        String token = AppPreferenceUtil.getToken(this);
//        if (mUrl != null) {
//            final int index = mUrl.indexOf('?');
//            if (index <= 0) {
//                String url = mUrl + "?userID=" + userID + "&token=" + token;
//                System.out.println(url);
//                web_simple_view.loadUrl(url);
//            } else {
//                String url = mUrl.substring(0, index) + "?userID=" + userID + "&token=" + token + "&" + mUrl.substring(index + 1);
//                System.out.println(url);
//                web_simple_view.loadUrl(url);
//            }
//        }
        if(mUrl.equals(Const.SHARE_URL)){
            mUrl = mUrl+"&uid="+Const.userid+"&token="+Const.token;
            ToastUtil.showToast(mUrl);
        }
        web_simple_view.loadUrl(mUrl);
    }

    @Override
    public void onBackPressed() {
        //super.onBackPressed();
        if (web_simple_view.canGoBack()) {
            web_simple_view.goBack();
        } else super.onBackPressed();
    }
}
